package com.jeesite.modules.task.status.service;

import java.util.Date;
import java.util.List;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.task.status.entity.DcSupTaskStatus;
import com.jeesite.modules.task.status.dao.DcSupTaskStatusDao;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * 任务状态变更表Service
 * @author zrr
 * @version 2022-03-02
 */
@Service
@Transactional(readOnly=false)
public class DcSupTaskStatusService extends CrudService<DcSupTaskStatusDao, DcSupTaskStatus> {

	@Autowired
	private DcSupTaskStatusDao dcSupTaskStatusDao;
	/**
	 * 获取单条数据
	 * @param dcSupTaskStatus
	 * @return
	 */
	@Override
	public DcSupTaskStatus get(DcSupTaskStatus dcSupTaskStatus) {
		return super.get(dcSupTaskStatus);
	}
	
	/**
	 * 查询分页数据
	 * @param dcSupTaskStatus 查询条件
	 * @param dcSupTaskStatus.page 分页对象
	 * @return
	 */
	@Override
	public Page<DcSupTaskStatus> findPage(DcSupTaskStatus dcSupTaskStatus) {
		return super.findPage(dcSupTaskStatus);
	}
	
	/**
	 * 查询列表数据
	 * @param dcSupTaskStatus
	 * @return
	 */
	@Override
	public List<DcSupTaskStatus> findList(DcSupTaskStatus dcSupTaskStatus) {
		return super.findList(dcSupTaskStatus);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param dcSupTaskStatus
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(DcSupTaskStatus dcSupTaskStatus) {
		super.save(dcSupTaskStatus);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(dcSupTaskStatus, dcSupTaskStatus.getId(), "dcSupTaskStatus_image");
		// 保存上传附件
		FileUploadUtils.saveFileUpload(dcSupTaskStatus, dcSupTaskStatus.getId(), "dcSupTaskStatus_file");
	}
	
	/**
	 * 更新状态
	 * @param dcSupTaskStatus
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(DcSupTaskStatus dcSupTaskStatus) {
		super.updateStatus(dcSupTaskStatus);
	}
	
	/**
	 * 删除数据
	 * @param dcSupTaskStatus
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(DcSupTaskStatus dcSupTaskStatus) {
		super.delete(dcSupTaskStatus);
	}

	/**
	 * 插入数据到状态表
	 * @param targetId
	 * @param targetType
	 * @param statusCode
	 * @param statusName
	 */
	public void addStatus(String targetId,String targetType,String statusCode,String statusName){
		DcSupTaskStatus dcSupTaskStatus = new DcSupTaskStatus();
		dcSupTaskStatus.setTargetId(targetId);
		dcSupTaskStatus.setTargetType(targetType);
		dcSupTaskStatus.setStatusCode(statusCode);
		dcSupTaskStatus.setStatusName(statusName);

		dcSupTaskStatus.setChangeBy(StringUtils.isBlank(UserUtils.getUser().getUserCode())?"system":UserUtils.getUser().getUserCode());
		dcSupTaskStatus.setChangeTime(new Date());
		dcSupTaskStatus.setCreateTime(new Date());
		dcSupTaskStatus.setUpdateTime(new Date());
		super.insert(dcSupTaskStatus);
	}

	/**
	 * 查询状态列表
	 * @param targetId
	 * @param targetType
	 * @param statusCode
	 * @return
	 */
	public List<DcSupTaskStatus> getStatusList(String targetId,Integer targetType,Integer statusCode){
		DcSupTaskStatus dcSupTaskStatus = new DcSupTaskStatus();
		dcSupTaskStatus.setTargetId(targetId);
		dcSupTaskStatus.setTargetType(targetType.toString());
		dcSupTaskStatus.setStatusCode(statusCode.toString());
		return super.findList(dcSupTaskStatus);
	}

	public List<DcSupTaskStatus> getStatusList(String targetId){
		DcSupTaskStatus dcSupTaskStatus = new DcSupTaskStatus();
		dcSupTaskStatus.setTargetId(targetId);
		return super.findList(dcSupTaskStatus);
	}

	/**
	 * 查询单条状态
	 * @param targetId
	 * @param targetType
	 * @param statusCode
	 * @return
	 */
	public DcSupTaskStatus getStatus(String targetId,Integer targetType,Integer statusCode){
		return dcSupTaskStatusDao.getStatus(targetId,targetType,statusCode);
	}
}